package combat.unit;

public class Actor {
  private int guid;
  private String name;
  private int flags;

  public Actor(String combined) {
    String[] t = combined.split(",");
    new Actor(t[0], t[1], t[2]);
  }

  public Actor(int guid, String name, int flags) {
    this.guid = guid;
    this.name = name;
    this.flags = flags;
  }

  public Actor(String guid, String name, String flags) {
    guid = fixString(guid);
    flags = fixString(flags);
    int intGuid = Integer.parseInt(guid.substring(2), 16);
    int intFlags = Integer.parseInt(flags.substring(2), 16);

    new Actor(intGuid, name, intFlags);
  }

  /**
   * If a numberic field in the combat log exceeds the value of a signed int, java doesn't
   * want to parse it. This fixes the string by making it negative.
   * @param s the potentially broken string
   */
  private String fixString(String s) {
    if (Integer.parseInt(Character.toString(s.charAt(2)), 16) > 7) {
      String temp = new String("0x-");
      temp += Integer.parseInt(Character.toString(s.charAt(2)), 16) - 8;
      temp += Long.toHexString((Long.parseLong("0" + s.substring(3), 16) + 1));
      return temp;

    }
    return s;

  }

  public int getGUID() {
    return guid;
  }

  public String getName() {
    return name;
  }

  public int getFlags() {
    return flags;
  }

  public String toString() {
    return "0x" + Integer.toHexString(guid) + "," + name + "," + "0x"
        + Integer.toHexString(flags);
  }
}
